package leetcode.D200.T114;

import util.TreeNode;

public class Solution {
    // 一刷
    /*public void flatten(TreeNode root) {
        if(root == null) {
            return;
        }
        flatten(root.right);
        if(root.left == null) {
            return;
        }
        flatten(root.left);
        TreeNode temp = root.left;
        while(temp.right != null) {
            temp = temp.right;
        }
        temp.right = root.right;
        root.right = root.left;
        root.left = null;
    }*/

    // 二刷
    /*public void flatten(TreeNode root) {
        if (root == null)
            return;
        flatten(root.left);
        flatten(root.right);
        TreeNode temp = root.right, p = root;
        root.right = root.left;
        root.left = null;
        while (p.right != null)
            p = p.right;
        p.right = temp;
    }*/

    // 三刷
    public void flatten(TreeNode root) {
        if (root == null)
            return;
        flatten(root.left);
        flatten(root.right);
        TreeNode temp = root.right, p = root;
        root.right = root.left;
        root.left = null;
        while (p.right != null)
            p = p.right;
        p.right = temp;
    }
}
